package com.zshan.clinic.mini.controller;


import com.zshan.clinic.admin.entity.vo.ClinicDeptSelectVo;
import com.zshan.clinic.admin.entity.vo.ClinicRegisterVo;
import com.zshan.clinic.admin.entity.vo.DoctorRemainVo;
import com.zshan.clinic.admin.service.customer.AppointmentService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.database.model.ClinicRegister;
import com.zshan.clinic.mini.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;


/**
 * 预约
 * @author lichen
 */
@RestController
@RequestMapping("/appointment")
public class AppointmentController extends AbstractController {


    @Autowired
    private AppointmentService appointmentService;


    /**
     * 获取科室列表
     * @return
     */
    @GetMapping("/getDeptList")
    public RestResp<List<ClinicDeptSelectVo>> getDeptList(){
        UserInfo userInfo = getUserInfo();
        List<ClinicDeptSelectVo> selectList = appointmentService.getDeptList(userInfo.getClinicId());
        return RestResp.SUCCESS(selectList);
    }

    /**
     * 查询诊所余号列表
     * @param deptId
     * @return
     */
    @GetMapping("/getRemainList")
    public RestResp<List<DoctorRemainVo>> getRemainList(@RequestParam(name = "deptId") Long deptId,
                                                        @RequestParam(name = "visitDate") LocalDate visitDate) {
        UserInfo userInfo = getUserInfo();
        List<DoctorRemainVo> remainList = appointmentService.getRemainList(userInfo.getClinicId(),deptId,visitDate);
        return RestResp.SUCCESS(remainList);
    }


    /**
     * 查询我的预约列表
     * @param regStatus 预约状态 regStatusEnum
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getAppointmentList")
    public RestResp<List<ClinicRegisterVo>> getAppointmentList(@RequestParam(name = "regStatus",required = false) String regStatus,
                                                               @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                                               @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        UserInfo userInfo = getUserInfo();
        List<ClinicRegisterVo> appointmentList = appointmentService.getAppointmentList(userInfo.getClinicId(),userInfo.getCmUserId(),regStatus,pageNo, pageSize);
        return RestResp.SUCCESS(appointmentList);
    }



    /**
     * 发起预约
     * @param clinicRegister
     * @return
     */
    @PostMapping("/addAppointment")
    public RestResp<Void> addAppointment(@RequestBody ClinicRegister clinicRegister) {
        UserInfo userInfo = getUserInfo();
        clinicRegister.setCmUserId(userInfo.getCmUserId());
        clinicRegister.setCreateUserId(userInfo.getCmUserId());
        clinicRegister.setCreateBy(userInfo.getNickName());
        clinicRegister.setClinicId(userInfo.getClinicId());
        appointmentService.addAppointment(clinicRegister);
        return RestResp.SUCCESS();
    }

    /**
     * 取消预约
     * @param clinicRegister
     * @return
     */
    @PostMapping("/cancelAppointment")
    public RestResp<Void> cancelAppointment(@RequestBody ClinicRegister clinicRegister) {
        UserInfo userInfo = getUserInfo();
        clinicRegister.setUpdateUserId(userInfo.getCmUserId());
        clinicRegister.setUpdateBy(userInfo.getNickName());
        clinicRegister.setClinicId(userInfo.getClinicId());
        appointmentService.cancelAppointment(clinicRegister);
        return RestResp.SUCCESS();
    }
}
